package self.niu.com.resume.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.melnykov.fab.FloatingActionButton;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import self.niu.com.resume.AppBase;
import self.niu.com.resume.R;
import self.niu.com.resume.activity.WebVeiwActivity;
import self.niu.com.resume.db.JavaAbility;
import self.niu.com.resume.db.KnowAbility;
import self.niu.com.resume.db.SeniorAbility;
import self.niu.com.resume.db.SkilledAbility;
import self.niu.com.resume.db.ToolsAbility;
import self.niu.com.resume.db.dbHeple.DbHander;
import self.niu.com.resume.ui.MyUtils;
import self.niu.com.resume.ui.ProgressUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyAbilitysFragment<T> extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private View view;
    private String type;
    @ViewInject(R.id.my_allability_lv)
    private ListView listView;
    private SimpleAdapter adapter;
    private List<T> datas = new ArrayList<>();
    ;
    private List<Map<String, Object>> mapDatas;
    private boolean isfrist;
    @ViewInject(R.id.my_allability_fab)
    FloatingActionButton fab;
    private Handler handler = new Handler();

    public MyAbilitysFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString("type");
        }
    }

    public static MyAbilitysFragment getInstance(String type) {
        MyAbilitysFragment my = new MyAbilitysFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        my.setArguments(bundle);
        return my;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my_abilitys, container, false);
        ViewUtils.inject(this, view);
        initView();
        return view;
    }

    private void initView() {
        mapDatas = new ArrayList<>();
        datas = new ArrayList<>();
        getDataformDb();
        fab.attachToListView(listView);
        fab.setOnClickListener(this);
        adapter = new SimpleAdapter(
                AppBase.getContext(),
                mapDatas,
                R.layout.all_item_layout,
                new String[]{"image", "title"},
                new int[]{R.id.all_head_img,
                        R.id.all_content_tv}
        );
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    public void getMapData() {
        if ("java".equals(type)) {
            for (int i = 0; i < MyUtils.allJavaTitls.length; i++) {
                Map<String, Object> map = new HashMap<>();
                JavaAbility java = (JavaAbility) datas.get(i);
                map.put("image", java.getImageUrl());
                map.put("title", java.getTitle());
                mapDatas.add(map);
            }
        } else if ("know".equals(type)) {
            for (int i = 0; i < MyUtils.allKnowTitls.length; i++) {
                Map<String, Object> map = new HashMap<>();
                KnowAbility konw = (KnowAbility) datas.get(i);
                map.put("image", konw.getImgUrl());
                map.put("title", konw.getTitle());
                mapDatas.add(map);
            }
        } else if ("senior".equals(type)) {
            for (int i = 0; i < MyUtils.allSeniorTitls.length; i++) {
                Map<String, Object> map = new HashMap<>();
                SeniorAbility senior = (SeniorAbility) datas.get(i);
                map.put("image", senior.getImgUrl());
                map.put("title", senior.getTitle());
                mapDatas.add(map);
            }
        } else if ("skilled".equals(type)) {
            for (int i = 0; i < MyUtils.allSkillTitls.length; i++) {
                Map<String, Object> map = new HashMap<>();
                SkilledAbility skilled = (SkilledAbility) datas.get(i);
                map.put("image", skilled.getImgUrl());
                map.put("title", skilled.getTitle());
                mapDatas.add(map);
            }
        } else if ("tool".equals(type)) {
            for (int i = 0; i < MyUtils.allToolTitls.length; i++) {
                Map<String, Object> map = new HashMap<>();
                ToolsAbility tool = (ToolsAbility) datas.get(i);
                map.put("image", tool.getImgurl());
                map.put("title", tool.getTitle());
                mapDatas.add(map);
            }
        }
        adapter.notifyDataSetChanged();
    }

    //从数据库中得到数据
    public List<T> getDataformDb() {
        new MyThread(type).start();
        ProgressUtils.getInstances().hideProgress();
        return datas;
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(getActivity(), "你要添加新的技能", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String url = "";
        switch (type) {
            case "java":
                JavaAbility java = DbHander.getInstance().findJavaAbilityAll().get(position);
                url = java.getWebUrl();
                break;
            case "know":
                KnowAbility konw = DbHander.getInstance().findKnowAbilityAll().get(position);
                url = konw.getWebUrl();
                break;
            case "skilled":
                SkilledAbility skilled = DbHander.getInstance().findSkilledAbilityAll().get(position);
                url = skilled.getWebUrl();
                break;
            case "senior":
                SeniorAbility senior = DbHander.getInstance().findSeniorAbilityAll().get(position);
                url = senior.getWebUrl();
                break;
            case "tool":
                ToolsAbility tool = DbHander.getInstance().findToolAbilityAll().get(position);
                url = tool.getWebUrl();
                break;
        }
        startActivity(new Intent(getActivity(), WebVeiwActivity.class).putExtra("url", url));
    }

    class MyThread extends Thread {
        private String type;

        private MyThread(String type) {
            this.type = type;
        }

        @Override
        public void run() {
            super.run();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    datas.clear();
                    if ("java".equals(type)) {
                        datas.addAll((Collection<? extends T>) DbHander.getInstance().findJavaAbilityAll());
                    } else if ("know".equals(type)) {
                        datas.addAll((Collection<? extends T>) DbHander.getInstance().findKnowAbilityAll());
                    } else if ("senior".equals(type)) {
                        datas.addAll((Collection<? extends T>) DbHander.getInstance().findSeniorAbilityAll());
                    } else if ("skilled".equals(type)) {
                        datas.addAll((Collection<? extends T>) DbHander.getInstance().findSkilledAbilityAll());
                    } else if ("tool".equals(type)) {
                        datas.addAll((Collection<? extends T>) DbHander.getInstance().findToolAbilityAll());
                    }
                    getMapData();
                }
            });
        }
    }
}





